package com.lcw.library.imagepicker.activity;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.lcw.library.imagepicker.R;
import com.lcw.library.imagepicker.utils.StatusBarUtil;

/**
 * BaseActivity基类
 * Create by: chenWei.li
 * Date: 2018/10/9
 * Time: 下午11:34
 * Email: lichenwei.me@foxmail.com
 */
public abstract class BaseActivity extends AppCompatActivity {

    private View mView;
    private ReceiveBroadCast receiveBroadCast;

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.tool_bar_color));

            StatusBarUtil.setRootViewFitsSystemWindows(this, true, true);
            if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
                StatusBarUtil.setStatusBarColor(this, 0x55000000);
            }
        }

        initStatusBar();

        if (mView == null) {
            mView = View.inflate(this, bindLayout(), null);
        }

        setContentView(mView);

        initConfig();
        initView();
        initListener();
        getData();

        //注册接收的广播
        receiveBroadCast = new ReceiveBroadCast();
        IntentFilter filter = new IntentFilter();
        filter.addAction("module_out_app_shiqin");
        registerReceiver(receiveBroadCast, filter);
    }

    /**
     * 声明接收扫描结果的广播，
     */
    class ReceiveBroadCast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("module_out_app_shiqin".equals(intent.getAction())) {
                Log.e("log","接受退出指令");
                finish();
            }
        }
    }

    /**
     * 处理界面布局
     */
    private void initStatusBar() {
        StatusBarUtil.setRootViewFullscreen(this, false);
        StatusBarUtil.setRootViewFitsSystemWindows(this, true, true);
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            StatusBarUtil.setStatusBarColor(this, 0x55000000);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (receiveBroadCast != null) {
            unregisterReceiver(receiveBroadCast);
        }
    }

    protected abstract int bindLayout();

    protected void initConfig() {

    }

    protected abstract void initView();

    protected abstract void initListener();

    protected abstract void getData();


}
